/**
 * @date 3:47:23 PM
 * StudentDaoImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.bean.dao.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;
import com.qs.education.bean.dao.IEducationStudentDao;
import com.qs.education.bean.dao.impl.BaseDaoImpl;
import com.qs.education.bean.entity.EducationStudent;

/**
 * @class StudentDaoImpl	
 * @author Warren
 * @description
 * @date Oct 31, 2018 
 */
@Repository
public class EducationStudentDaoImpl extends BaseDaoImpl<EducationStudent> implements IEducationStudentDao{
	/**
	 * @author Warren
	 * @param mobile
	 * @param password
	 * @return EducationStudent
	 * @description 根据手机号码和密码得到当前学生
	 * @date Oct 31, 2018
	 */
	public EducationStudent getEducationStudent(String mobile, String password) {
		String jql = "Select es from EducationStudent es where es.mobile=:mobile and es.password=:password";
		Query query = this.em.createQuery(jql);
		query.setParameter("mobile", mobile);
		query.setParameter("password", password);
		@SuppressWarnings("unchecked")
		List<EducationStudent> list = query.getResultList();
		if(list !=null && list.size()>0) {
			return list.get(0);
		}else {
			return null;
		}
	}
	
	/**
	 * @author Warren
	 * @param mobile
	 * @return boolean
	 * @description 根据手机号查询学生
	 * @date Nov 6, 2018
	 */
	@Override
	public EducationStudent findEducationStudentByMobile(String mobile) {
		String jql = "from EducationStudent es where es.mobile =:mobile";
		Map<String, Object> params = new HashMap<>();
		params.put("mobile", mobile);
		List<EducationStudent> result = query(jql, EducationStudent.class, params);
		if(null != result && !result.isEmpty()){
			return result.get(0);
		}
		return null;
	}

	@Override
	public List<EducationStudent> findStudentByCondition(EducationStudent student) {
		String jql = "from EducationStudent es where 1 = 1";
		Map<String, Object> params = new HashMap<>();
		if (student.getSchool()!=null&&student.getSchool().getName()!=null&&student.getSchool().getName()!="") {
			jql+=" and es.school.name like :school";
			params.put("school", "%"+student.getSchool().getName()+"%");
		}
		if (student.getMobile()!=null&&student.getMobile()!="") {
			jql+=" and es.mobile like :mobile";
			params.put("mobile", "%"+student.getMobile()+"%");
		}
		if (student.getName()!=null&&student.getName()!="") {
			jql+=" and es.name like :name";
			params.put("name", "%"+student.getName()+"%");
		}
		if (student.getGrade()!=null&&student.getGrade().getId()!=null) {
			jql+=" and es.grade = :grade";
			params.put("grade", student.getGrade());
		}
		List<EducationStudent> result = query(jql, EducationStudent.class, params);
		
		return result;
	}

	public int getStudentCount(EducationStudent student) {
		
		String jql = "Select COUNT(*)  from EducationStudent es where 1 = 1";
		Map<String, Object> params = new HashMap<>();
		if (student.getSchool()!=null&&student.getSchool().getName()!=null&&student.getSchool().getName()!="") {
			jql+=" and es.school.name like :school";
			params.put("school", "%"+student.getSchool().getName()+"%");
		}
		if (student.getMobile()!=null&&student.getMobile()!="") {
			jql+=" and es.mobile like :mobile";
			params.put("mobile", "%"+student.getMobile()+"%");
		}
		if (student.getName()!=null&&student.getName()!="") {
			jql+=" and es.name like :name";
			params.put("name", "%"+student.getName()+"%");
		}
		if (student.getGrade()!=null&&student.getGrade().getId()!=null) {
			jql+=" and es.grade = :grade";
			params.put("grade", student.getGrade());
		}
		List<?> result = query(jql,params);
		if(null!=result && !result.isEmpty()) {
			return Integer.parseInt(result.get(0).toString());
		}
		return 0;
	}

	@Override
	public List<EducationStudent> getStudent(EducationStudent student, Integer currentPage, Integer pageSize) {
		
		String jql = "Select es from EducationStudent es where 1 = 1";
		Map<String, Object> params = new HashMap<>();
		if (student.getSchool()!=null&&student.getSchool().getName()!=null&&student.getSchool().getName()!="") {
			jql+=" and es.school.name like :school";
			params.put("school", "%"+student.getSchool().getName()+"%");
		}
		if (student.getMobile()!=null&&student.getMobile()!="") {
			jql+=" and es.mobile like :mobile";
			params.put("mobile", "%"+student.getMobile()+"%");
		}
		if (student.getName()!=null&&student.getName()!="") {
			jql+=" and es.name like :name";
			params.put("name", "%"+student.getName()+"%");
		}
		if (student.getGrade()!=null&&student.getGrade().getId()!=null) {
			jql+=" and es.grade = :grade";
			params.put("grade", student.getGrade());
		}
		int firstResult = (currentPage - 1) * pageSize;
		List<EducationStudent> list = query(jql, EducationStudent.class, firstResult, pageSize, params);
		
		return list;
	};
	
}
